package com.pccw.httpclient;

import java.io.IOException;
import java.io.Serializable;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.SerializableEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import com.acrm.client.entity.staff.Staff;


public class Demo {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws ClientProtocolException, IOException {
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setContentCharset(params, "utf-8");
		HttpProtocolParams.setHttpElementCharset(params, "utf-8");
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setUserAgent(params, "utf-8");
		HttpConnectionParams.setConnectionTimeout(params,5000);
		HttpConnectionParams.setSoTimeout(params, 5000);
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 111));
		//schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
		ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params,schemeRegistry);
		HttpClient client=new DefaultHttpClient(connectionManager,params);
		
		//HttpPost post=new HttpPost("http://localhost:8080/XCFServer1");
		HttpPost post=new HttpPost("http://localhost:8080/CRMServer/test/ctaclist.action");
		Staff s=new Staff();
		s.setStaffName("guxuede");
		SerializableEntity entity=new SerializableEntity(s, true);
		post.setEntity(entity);
		System.out.println("start");
		client.execute(post, new ResponseHandler<Void>() {
			@Override
			public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(response);
				return null;
			}
		});
		System.out.println("end");
	}

}
